package org.example;


import javafx.fxml.FXML;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.example.constant.Resource;

import java.io.File;

public class SettingsController {
    Stage mStage;
    @FXML
    private Tab tab1,tab2,tab3,tab4,tab5;

    @FXML
    private TextField tvPathYlog, tvPathVideo, tvPathApk, tvPathTrace, tvPathPkgInfo;

    public void initialize() {
        tab1.setText(Resource.MENU_YLOG);
        tab2.setText(Resource.MENU_JIT);
        tab3.setText(Resource.MENU_APP_START);
        tab4.setText(Resource.MENU_TRACE);
        tab5.setText(Resource.MENU_SCREEN_RECORD);

        initUIByConfig();
    }

    private void initUIByConfig(){
        tvPathYlog.setText(App.mConfig.get("path_ylog"));
        tvPathVideo.setText(App.mConfig.get("path_video"));
        tvPathApk.setText(App.mConfig.get("path_apk"));
        tvPathTrace.setText(App.mConfig.get("path_trace"));
    }

    @FXML
    void onOpenFolderTrace(MouseEvent event) { tvPathTrace.setText(onOpenFolder()); }

    @FXML
    void onOpenFolderApk(MouseEvent event) {
        tvPathApk.setText(onOpenFolder());
    }

    @FXML
    void onOpenFolderVideo(MouseEvent event) {
        tvPathVideo.setText(onOpenFolder());
    }

    @FXML
    void onOpenFolderYlog(MouseEvent event) {
        tvPathYlog.setText(onOpenFolder());
    }

    @FXML
    void onOpenFolderPkgInfo(MouseEvent event) {
        tvPathPkgInfo.setText(onOpenFolder());
    }

    private String onOpenFolder() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("选择一个目录");
        File fSelectPath = directoryChooser.showDialog(mStage);
        if (fSelectPath == null) {
            return "";
        }
        String path = fSelectPath.getPath();
        return path;
    }

    @FXML
    void onCancel(MouseEvent event) {
        mStage.close();
    }

    @FXML
    void onSave(MouseEvent event) {
        App.mConfig.put("path_ylog", tvPathYlog.getText());
        App.mConfig.put("path_video", tvPathVideo.getText());
        App.mConfig.put("path_apk", tvPathApk.getText());
        App.mConfig.put("path_trace", tvPathTrace.getText());
        App.mConfig.put("path_pkg_info", tvPathPkgInfo.getText());
    }

    @FXML
    void onSaveAndQuit(MouseEvent event) {
        onSave(event);
        onCancel(event);
    }

    public void setStage(Stage stage) {
        mStage = stage;
    }
}
